import { createRouter, createWebHistory } from 'vue-router'
import { heightRoutes, hMenuItems } from './height'
import { primaryRoutes, pMenuItems } from './primary'
//'primary', 'middle', 'height',
const schoolType = 'primary'
const list = [{
  routes: primaryRoutes,
  menuItems: pMenuItems,
  type: 'primary',
}, {
  routes: heightRoutes,
  menuItems: hMenuItems,
  type: 'height',
}]
const getListItem = (type:any) => {
  return list.find(item => item.type === type)
}
let routesItem:any = getListItem(schoolType)
// 动态生成路由规则的函数
function generateRoutesFromMenu(menuData:any, basePath = '') {
  let routes:any = [];
  menuData.forEach((item:any) => {
    // 如果有子菜单，则递归生成路由
    if (item.children && item.children.length) {
      routes = routes.concat(generateRoutesFromMenu(item.children, `${basePath}/${item.name}`));
    } else {
      // 如果没有子菜单，则创建一个基本的路由规则
      routes.push({
        path: `${basePath}/${item.name}`,
        name: item.name,
        component: () => import(`@/views/${item.link}.vue`), // 动态导入组件
      });
    }
  });
  return routes;
}

// 使用函数生成路由规则
const dynamicRoutes = generateRoutesFromMenu(routesItem.menuItems);
// 将动态路由规则添加到路由实例中


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routesItem.routes,
})
dynamicRoutes.forEach((route:any) => {
  router.addRoute(route);
});
export default router
